﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="DownSong.cs" company="3po.ru">
//   Author: Alexander Ulitin [support@3po.ru]
//   
//   This file is part of Conusic.
//   
//   Conusic is free software: you can redistribute it and/or modify
//   it under the terms of the GNU General Public License as published by
//   the Free Software Foundation, either version 3 of the License, or
//   (at your option) any later version.
//   
//   Conusic is distributed in the hope that it will be useful,
//   but WITHOUT ANY WARRANTY; without even the implied warranty of
//   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//   GNU General Public License for more details.
//   
//   You should have received a copy of the GNU General Public License
//   along with Conusic.  If not, see http://www.gnu.org/licenses/.
// </copyright>
// <summary>
//   The file song download.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace MusicPage
{
    using System;

    using Conusic;

    /// <summary>
    /// The file song download.
    /// </summary>
    /// <param name="record">
    /// The record.
    /// </param>
    internal delegate void FileSongDownload(AudioRecord record);

    /// <summary>
    /// The file song fail.
    /// </summary>
    /// <param name="record">
    /// The record.
    /// </param>
    /// <param name="ext">
    /// The ext.
    /// </param>
    internal delegate void FileSongFail(AudioRecord record, Exception ext);

    /// <summary>
    /// The file song progress.
    /// </summary>
    /// <param name="record">
    /// The record.
    /// </param>
    /// <param name="downloadKb">
    /// The download kb.
    /// </param>
    /// <param name="totalKb">
    /// The total kb.
    /// </param>
    internal delegate void FileSongProgress(AudioRecord record, long downloadKb, long totalKb);

    /// <summary>
    /// mediator for connect gui application events and commands from conusic api
    /// </summary>
    internal class DownSong : IDownloadSongCommand
    {
        #region Static Fields

        /// <summary>
        /// The update progress.
        /// </summary>
        private static readonly TimeSpan UpdateProgress = new TimeSpan(0, 0, 0, 0, 500);

        #endregion

        #region Fields

        /// <summary>
        /// The record.
        /// </summary>
        private readonly AudioRecord record;

        /// <summary>
        /// The last update time.
        /// </summary>
        private DateTime lastUpdateTime = DateTime.MinValue;

        #endregion

        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="DownSong"/> class.
        /// </summary>
        /// <param name="record">
        /// The record.
        /// </param>
        public DownSong(AudioRecord record)
        {
            this.record = record;
        }

        #endregion

        #region Public Events

        /// <summary>
        /// The on download fail.
        /// </summary>
        public event FileSongFail OnDownloadFail;

        /// <summary>
        /// The on progress.
        /// </summary>
        public event FileSongProgress OnProgress;

        /// <summary>
        /// The on song download.
        /// </summary>
        public event FileSongDownload OnSongDownload;

        #endregion

        #region Public Methods and Operators

        /// <summary>
        /// The fail.
        /// </summary>
        /// <param name="ext">
        /// The ext.
        /// </param>
        public void Fail(Exception ext)
        {
            this.OnDownloadFail(this.record, ext);
        }

        /// <summary>
        /// The progress execute.
        /// </summary>
        /// <param name="downloadedKb">
        /// The downloaded kb.
        /// </param>
        /// <param name="totalKb">
        /// The total kb.
        /// </param>
        public void ProgressExecute(long downloadedKb, long totalKb)
        {
            if (DateTime.Now - this.lastUpdateTime > UpdateProgress)
            {
                this.OnProgress(this.record, downloadedKb, totalKb);
                this.lastUpdateTime = DateTime.Now;
            }
        }

        /// <summary>
        /// The success.
        /// </summary>
        public void Success()
        {
            this.OnSongDownload(this.record);
        }

        #endregion
    }
}